import Bezier from 'bezier';

var GameData = require('../module/gameData')();

cc.Class({
    extends: cc.Component,
    properties: {},
    onLoad() {
        this.id = 1;
        this.init();

    },
    init() {
        var _Allfish = this.node.parent.parent.parent;
        if (_Allfish.cr_fish) {

        } else {
            var bezierid = 1;

            this.getMovePath(_Allfish, "bezier_id_" + _Allfish.bezierid);
            _Allfish.moveStop = false;
        }


        console.log("ssss", this.node.parent.name)
    },
    getMovePath(sp, moveid) {
        sp.time = 5;
        sp.moveArr = [];
        sp.moveid = moveid;
        sp.moveMaxidx = GameData.moveData[moveid].length;
        sp.runTime = 0;
        let bezier = GameData.moveData[moveid][sp.movidx];
        sp.moveArr = Bezier(bezier, 100, sp.time);
        if (sp.sceneType) {
            sp.time = 10;
            sp.moveArr = [];
            sp.moveid = moveid;
            sp.moveMaxidx = GameData.moveData[moveid].length;
            sp.runTime = 0;
            let bezier1 = GameData.moveData[moveid][sp.movidx];
            sp.moveArr = Bezier(bezier1, 100, sp.time);
        }
        if (sp.movidx == 0) {
            sp.lastPoint = cc.v2(GameData.moveData[moveid][sp.movidx][0].x, GameData.moveData[moveid][sp.movidx][0].y);
        }
    },
    AllfishMove(dt) {
        var sp = this.node.parent.parent.parent;
        if (sp.eatMove) {
            sp.eatTime += dt;
            if (sp.eatback) {
                if (sp.eatTime <= sp.eat_time) {
                    let position1 = sp.eatArr.getPoint(sp.eatTime);
                    var curPoint = sp.getPosition();  //获取鱼当前坐标
                    this.restAngle(sp, sp.E_end_point, curPoint);
                    sp.E_end_point = curPoint;  //保存当前的坐标给下一轮刷新使用
                    sp.position = position1;
                } else {
                    sp.eatMove = false;
                    sp.moveStop = false;
                }
            }
            else {
                if (GameData.GameLayer.particle2.node.scaleX == 0) {
                    var _rota = sp.rotation;
                    var type;
                    if (_rota > -270 && _rota > -90) {
                        type = 4
                    } else {
                        type = 1
                    }
                    var end_p = sp.getPosition();
                    /*当前鱼的位置*/
                    var start_p = sp.end_p;
                    /*鱼去鱼饵  鱼饵的位置*/
                    var z_p = GameData.GameLayer.getzhongxian(end_p, start_p, 100, type);
                    var bezier = [
                        {"x": end_p.x, "y": end_p.y},
                        {"x": z_p.x, "y": z_p.y},
                        {"x": start_p.x, "y": start_p.y},
                    ];
                    GameData.GameLayer.initeatMove(sp, bezier);
                    sp.eatback = true;
                    sp.iseat = false;
                } else {
                    sp.getChildByName("Allfish").rotation = -90;
                    sp.position = GameData.GameLayer.particle2.node.getPosition();
                }
            }
        }
        if (sp.backMove) {
            sp.backTime += dt;
            if (sp.backTime <= sp.back_time) {
                let position1 = sp.backArr.getPoint(sp.backTime);
                var curPoint = sp.getPosition();  //获取鱼当前坐标
                this.restAngle(sp, sp.B_end_point, curPoint);
                sp.B_end_point = curPoint;  //保存当前的坐标给下一轮刷新使用
                sp.position = position1;
            } else {
                sp.backMove = false;
                sp.moveStop = false;
            }
        }
        if (!sp.moveStop) {
            sp.runTime += dt;
            if (sp != 12 && sp != 13 && sp != 14) {
                if (sp.runTime <= sp.time) {
                    let position1 = sp.moveArr.getPoint(sp.runTime);
                    var curPoint = cc.v2(sp.x, sp.y);  //获取鱼当前坐标
                    this.restAngle(sp, sp.lastPoint, curPoint);
                    sp.lastPoint = curPoint;  //保存当前的坐标给下一轮刷新使用
                    sp.position = position1;
                } else {
                    sp.movidx++;
                    if (sp.movidx < sp.moveMaxidx) {
                        this.getMovePath(sp, sp.moveid);
                    } else {
                        if (sp.sceneType) {
                            GameData.gameHome.anginMove(sp);
                        } else {
                            GameData.GameLayer.anginMove(sp);
                        }
                    }
                }
            }
        }
    },
    restAngle(sp1, lastPoint, curPoint) {
        var sp = sp1.getChildByName("Allfish");
        var K = Math.atan(Math.abs((curPoint.y - lastPoint.y) / (curPoint.x - lastPoint.x)));
        var _jiaodu = K * 180 / 3.14159265;
        if (lastPoint.x <= curPoint.x) {
            if (lastPoint.y <= curPoint.y) {
                /* 第一象限 */
                // console.log("第一象限", _jiaodu);
                sp.rotation = (180 - _jiaodu);
                // sp.angle = -(180 - _jiaodu);
                sp.scaleY = -1;
            } else {
                /* 第四象限 */
                // console.log("第四象限", _jiaodu);
                sp.rotation = (-180 + _jiaodu);
                // sp.angle = -(-180 + _jiaodu);
                sp.scaleY = -1;
            }
        }
        else {
            if (lastPoint.y <= curPoint.y) {
                /* 第二象限 */
                // console.log("第二象限", _jiaodu);
                sp.rotation = (_jiaodu);
                // sp.angle = -(_jiaodu);
                sp.scaleY = 1;
            } else {
                /* 第三象限 */
                // console.log("第三象限", K * 180 / 3.14159265);
                sp.rotation = (-_jiaodu);
                // sp.angle = -(-_jiaodu);
                sp.scaleY = 1;
            }
        }
    },
    start() {
    },
    update(dt) {
        if (!GameData.isPause) {
            this.AllfishMove(dt);
        }
        var _Allfish = this.node.parent.parent.parent;
        if (_Allfish.isgo) {
            var pos11 = GameData.GameLayer.point1.node.parent.convertToWorldSpaceAR(GameData.GameLayer.point1.node.getPosition());
            var _pos1 = GameData.GameLayer.node.convertToNodeSpaceAR(pos11);
            var _x = _Allfish.clickPos.x;


            var y = this.getx1(_pos1.x, _pos1.y, GameData.GameLayer.bulletArr1[0].x, GameData.GameLayer.bulletArr1[0].y, _x);
            var p = cc.v2(_x, y);
            var pos1 = GameData.GameLayer.node.convertToWorldSpaceAR(_Allfish.clickPos);
            var pos = _Allfish.parent.convertToNodeSpaceAR(pos1);
            // var pos= _Allfish.parent.parent.convertToNodeSpaceAR(pos1);

            // _Allfish.parent.position=pos;

            _Allfish.x = pos.x;

            _Allfish.y = pos.y;
            // console.log( this.node.parent.getPosition())

            // console.log("sss",y,pos,GameData.GameLayer.bulletArr1[0].getPosition())
            // this.node.parent.y=y;
        }
    },
    getx1(x1, y1, x2, y2, x) {
        var y = (x - x1) * (y2 - y1) / (x2 - x1) + y1;
        return y;
    }
});
